package sg.tua.hud;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Transform;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

public class MoveableHudAnimation extends HudAnimation implements MouseListener {

	private Vector2f basePosition;
	boolean moving = false;
	boolean mouseOver = false;
	boolean moveable = true;
	boolean clicked = false;
	
	public MoveableHudAnimation(String name, Vector2f position, Animation animation, Shape collisionShape, int collisionType, boolean moveable){
		super(name, position,animation,collisionShape,collisionType);
		basePosition = position.copy();
		moving = false;
		this.moveable = moveable;
		getCollisionShape().setCenterX(position.x);
		getCollisionShape().setCenterY(position.y);	
	}
	@Override
	public void mouseClicked(int arg0, int x, int y, int arg3) {
		if (coordsInside(new Vector2f(x,y))) setClicked(true);
	}

	@Override
	public void mouseDragged(int oldx, int oldy, int newx, int newy) {
			if (mouseOver && moveable){
				if(newx > (getNormalCollisionShape().getWidth()/2) && newx<(1024-getNormalCollisionShape().getWidth()/2)){
					position.x = newx;
					moving = true;
				}		
				if(newy > (getNormalCollisionShape().getHeight()/2) && newy<(768-getNormalCollisionShape().getHeight()/2)){
					position.y = newy;
					moving = true;
				}
			} else moving = false;
	}

	@Override
	public void mouseMoved(int oldx, int oldy, int newx, int newy) {
		if (coordsInside(new Vector2f(newx,newy))) mouseOver = true; else mouseOver = false; 
	}

	@Override
	public void mousePressed(int btn, int x, int y) {
		;
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(int arg0, int newx, int newy) {
			moving = false;
			this.position = this.basePosition.copy();
			if (coordsInside(new Vector2f(newx,newy))) mouseOver = true; else mouseOver = false;
	}

	@Override
	public void mouseWheelMoved(int arg0) {
	}

	@Override
	public void inputEnded() {
		// TODO Auto-generated method stub

	}

	@Override
	public void inputStarted() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isAcceptingInput() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void setInput(Input arg0) {
		// TODO Auto-generated method stub

	}
	@Override 
	public void render(Graphics g){
		drawCenter(position.x,position.y);
		//g.draw(getCollisionShape());
	}
	public void drawCenter(float cX, float cY){
		//Draws from the middle instead of the upper left corner
		cX = cX-animation.getWidth()/2;
		cY = cY-animation.getHeight()/2;
		if (mouseOver) animation.setCurrentFrame(1); else animation.setCurrentFrame(0);
		animation.draw(cX,cY);
	}

	public boolean coordsInside(Vector2f coords){
		if (coords.x > getCollisionShape().getX() && coords.x < getCollisionShape().getX()+getCollisionShape().getWidth())
			if (coords.y > getCollisionShape().getY() && coords.y < getCollisionShape().getY()+getCollisionShape().getHeight())
				return true;
		return false;
	}
	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta) {
	}
	public boolean isClicked() {
		boolean tclick = clicked;
		clicked = false;
		return tclick;
	}
	public void setClicked(boolean clicked) {
		this.clicked = clicked;
	}
}
